﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ScriptEditor.Commands;

namespace ScriptEditor
{
    /// <summary>
    /// CommandUI.xaml 的交互逻辑
    /// </summary>
    public partial class CommandUI : UserControl
    {
        public const double cell = 45;
        public double size = 2;
        UIType type;
        public enum UIType
        {
            None,
            String,
            Int,
            Float,
            StringArray,
            IntArray,
            FloatArray,
            Bool,
            BoolArray,
            Vector3,
            Vector3Array,
            Vector2,
            Vector2Array,
            Color,
            ColorArray,
            ChoiceExp,
            ChoiceExpArray,
            Ease,
            Loop,
            Rotate,
            PathMode,
            TextAlignmentOptions,
            TYPE,
            PATH,
            GameState,
            EaseArray,
            LoopArray,
            RotateArray,
            PathModeArray,
            TextAlignmentOptionsArray,
            TYPEArray,
            PATHArray,
            GameStateArray,
        }

        public CommandUI()
        {
            InitializeComponent();
        }
        public CommandUI(string title, UIType UItype, object value)
        {
            InitializeComponent();
            CommandUIInit(title, type, value);
        }

        internal void CommandUIRe()
        {
            StringEditor.Visibility = Visibility.Hidden;
            BoolEditor.Visibility = Visibility.Hidden;
            Vector2Editor.Visibility = Visibility.Hidden;
            Vector3Editor.Visibility = Visibility.Hidden;
            ColorEditor.Visibility = Visibility.Hidden;
            ChoiceExpEditor.Visibility = Visibility.Hidden;
            EnumEditor.Visibility = Visibility.Hidden;
            StringArrayEditor.Visibility = Visibility.Hidden;
            BoolArrayEditor.Visibility = Visibility.Hidden;
            Vector2ArrayEditor.Visibility = Visibility.Hidden;
            Vector3ArrayEditor.Visibility = Visibility.Hidden;
            ColorArrayEditor.Visibility = Visibility.Hidden;
            ChoiceExpArrayEditor.Visibility = Visibility.Hidden;
            EnumArrayEditor.Visibility = Visibility.Hidden;
            size = 2;
        }

        public void CommandUIInit(string title, UIType UItype, object value)
        {
            CommandUIRe();
            Title.Header = title;
            type = UItype;
            switch (type)
            {
                case UIType.String:
                case UIType.Int:
                case UIType.Float:
                    {
                        StringEditor.Visibility = Visibility.Visible;
                        StringText.Text = value.ToString();
                    }
                    break;
                case UIType.StringArray:
                    {
                        StringArrayEditor.Visibility = Visibility.Visible;
                        StringArray.Items.Clear();
                        foreach (string obj in (string[])value)
                        {
                            StringArray.Items.Add(obj);
                        }
                    }
                    break;
                case UIType.IntArray:
                    {
                        StringArrayEditor.Visibility = Visibility.Visible;
                        StringArray.Items.Clear();
                        foreach (int obj in (int[])value)
                        {
                            StringArray.Items.Add(obj.ToString());
                        }
                    }
                    break;
                case UIType.FloatArray:
                    {
                        StringArrayEditor.Visibility = Visibility.Visible;
                        StringArray.Items.Clear();
                        foreach (float obj in (float[])value)
                        {
                            StringArray.Items.Add(obj.ToString());
                        }
                    }
                    break;
                case UIType.Bool:
                    {
                        BoolEditor.Visibility = Visibility.Visible;
                        BoolText.IsChecked = (bool)value;
                        size = 1;
                    }
                    break;
                case UIType.BoolArray:
                    {
                        BoolArrayEditor.Visibility = Visibility.Visible;
                        BoolArray.Items.Clear();
                        foreach(bool obj in (bool[])value)
                        {
                            BoolArray.Items.Add(obj.ToString());
                        }
                        size = 1.6;
                    }
                    break;
                case UIType.Vector3:
                    {
                        Vector3Editor.Visibility = Visibility.Visible;
                        Vector3 vector3 = (Vector3)value;
                        Vector3x.Text = vector3.x.ToString();
                        Vector3y.Text = vector3.y.ToString();
                        Vector3z.Text = vector3.z.ToString();
                    }
                    break;
                case UIType.Vector3Array:
                    {
                        Vector3ArrayEditor.Visibility = Visibility.Visible;
                        Vector3Array.Items.Clear();
                        foreach (Vector3 obj in (Vector3[])value)
                        {
                            Vector3Array.Items.Add(obj.x.ToString()+","+ obj.y.ToString() + ","+ obj.z.ToString());
                        }
                        size = 2.2;
                    }
                    break;
                case UIType.Color:
                    {
                        ColorEditor.Visibility = Visibility.Visible;
                        Color color = (Color)value;
                        Colorx.Text = color.r.ToString();
                        Colory.Text = color.g.ToString();
                        Colorz.Text = color.b.ToString();
                    }
                    break;
                case UIType.ColorArray:
                    {
                        ColorArrayEditor.Visibility = Visibility.Visible;
                        ColorArray.Items.Clear();
                        foreach (Color obj in (Color[])value)
                        {
                            ColorArray.Items.Add(obj.r.ToString() + "," + obj.g.ToString() + "," + obj.b.ToString());
                        }
                        size = 2.2;
                    }
                    break;
                case UIType.ChoiceExp:
                    {
                        ChoiceExpEditor.Visibility = Visibility.Visible;
                        ChoiceExp choice = (ChoiceExp)value;
                        ChoiceExpx.Text = choice.exp;
                        ChoiceExpy.Text = choice.jumpId;
                        ChoiceExpz.Text = choice.text;
                    }
                    break;
                case UIType.ChoiceExpArray:
                    {
                        ChoiceExpArrayEditor.Visibility = Visibility.Visible;
                        ChoiceExpArray.Items.Clear();
                        foreach (ChoiceExp obj in (ChoiceExp[])value)
                        {
                            ChoiceExpArray.Items.Add(obj.exp + "," + obj.jumpId + "," + obj.text);
                        }
                        size = 2.2;
                    }
                    break;
                case UIType.Vector2:
                    {
                        Vector2Editor.Visibility = Visibility.Visible;
                        Vector2 vector2 = (Vector2)value;
                        Vector2x.Text = vector2.x.ToString();
                        Vector2y.Text = vector2.y.ToString();
                    }
                    break;
                case UIType.Vector2Array:
                    {
                        Vector2ArrayEditor.Visibility = Visibility.Visible;
                        Vector2Array.Items.Clear();
                        foreach (Vector2 obj in (Vector2[])value)
                        {
                            Vector2Array.Items.Add(obj.x.ToString() + "," + obj.y.ToString());
                        }
                        size = 2.2;
                    }
                    break;
                case UIType.Ease:
                case UIType.Loop:
                case UIType.Rotate:
                case UIType.PathMode:
                case UIType.TextAlignmentOptions:
                case UIType.TYPE:
                case UIType.PATH:
                case UIType.GameState:
                    {
                        EnumEditor.Visibility = Visibility.Visible;
                        size = 1;
                        EnumText.Items.Clear();
                        foreach (string name in Enum.GetNames(UIType2Enum(type)))
                        {
                            EnumText.Items.Add(name);
                        }
                        EnumText.SelectedItem = value.ToString();
                    }
                    break;
                case UIType.EaseArray:
                    {
                        //从这里开始
                        EnumArrayEditor.Visibility = Visibility.Visible;
                        EnumArray.Items.Clear();
                        EnumArrayText.Items.Clear();
                        foreach (string name in Enum.GetNames(UIType2Enum(type)))
                        {
                            EnumArrayText.Items.Add(name);
                        }
                        foreach (Ease e in (Ease[])value)
                        {
                            EnumArray.Items.Add(Enum.GetName(UIType2Enum(type), e));
                        }
                    }
                    break;
                case UIType.LoopArray:
                    {
                        //从这里开始
                        EnumArrayEditor.Visibility = Visibility.Visible;
                        EnumArray.Items.Clear();
                        EnumArrayText.Items.Clear();
                        foreach (string name in Enum.GetNames(UIType2Enum(type)))
                        {
                            EnumArrayText.Items.Add(name);
                        }
                        foreach (Loop e in (Loop[])value)
                        {
                            EnumArray.Items.Add(Enum.GetName(UIType2Enum(type), e));
                        }
                    }
                    break;
                case UIType.RotateArray:
                    {
                        //从这里开始
                        EnumArrayEditor.Visibility = Visibility.Visible;
                        EnumArray.Items.Clear();
                        EnumArrayText.Items.Clear();
                        foreach (string name in Enum.GetNames(UIType2Enum(type)))
                        {
                            EnumArrayText.Items.Add(name);
                        }
                        foreach (Rotate e in (Rotate[])value)
                        {
                            EnumArray.Items.Add(Enum.GetName(UIType2Enum(type), e));
                        }
                    }
                    break;
                case UIType.PathModeArray:
                    {
                        //从这里开始
                        EnumArrayEditor.Visibility = Visibility.Visible;
                        EnumArray.Items.Clear();
                        EnumArrayText.Items.Clear();
                        foreach (string name in Enum.GetNames(UIType2Enum(type)))
                        {
                            EnumArrayText.Items.Add(name);
                        }
                        foreach (PathMode e in (PathMode[])value)
                        {
                            EnumArray.Items.Add(Enum.GetName(UIType2Enum(type), e));
                        }
                    }
                    break;
                case UIType.TextAlignmentOptionsArray:
                    {
                        //从这里开始
                        EnumArrayEditor.Visibility = Visibility.Visible;
                        EnumArray.Items.Clear();
                        EnumArrayText.Items.Clear();
                        foreach (string name in Enum.GetNames(UIType2Enum(type)))
                        {
                            EnumArrayText.Items.Add(name);
                        }
                        foreach(TextAlignmentOptions e in (TextAlignmentOptions[])value)
                        {
                            EnumArray.Items.Add(Enum.GetName(UIType2Enum(type), e));
                        }
                    }
                    break;
                case UIType.TYPEArray:
                    {
                        //从这里开始
                        EnumArrayEditor.Visibility = Visibility.Visible;
                        EnumArray.Items.Clear();
                        EnumArrayText.Items.Clear();
                        foreach (string name in Enum.GetNames(UIType2Enum(type)))
                        {
                            EnumArrayText.Items.Add(name);
                        }
                        foreach (TYPE e in (TYPE[])value)
                        {
                            EnumArray.Items.Add(Enum.GetName(UIType2Enum(type), e));
                        }
                    }
                    break;
                case UIType.PATHArray:
                    {
                        //从这里开始
                        EnumArrayEditor.Visibility = Visibility.Visible;
                        EnumArray.Items.Clear();
                        EnumArrayText.Items.Clear();
                        foreach (string name in Enum.GetNames(UIType2Enum(type)))
                        {
                            EnumArrayText.Items.Add(name);
                        }
                        foreach (PATH e in (PATH[])value)
                        {
                            EnumArray.Items.Add(Enum.GetName(UIType2Enum(type), e));
                        }
                    }
                    break;
                case UIType.GameStateArray:
                    {
                        //从这里开始
                        EnumArrayEditor.Visibility = Visibility.Visible;
                        EnumArray.Items.Clear();
                        EnumArrayText.Items.Clear();
                        foreach (string name in Enum.GetNames(UIType2Enum(type)))
                        {
                            EnumArrayText.Items.Add(name);
                        }
                        foreach (GameState e in (GameState[])value)
                        {
                            EnumArray.Items.Add(Enum.GetName(UIType2Enum(type), e));
                        }
                    }
                    break;
                default:
                    {

                    }
                    break;
            }
        }

        //通过UI种类获取类型
        public static Type UIType2Type(UIType type)
        {
            switch (type)
            {
                case UIType.String:
                    return typeof(string);
                case UIType.Int:
                    return typeof(int);
                case UIType.Float:
                    return typeof(float);
                case UIType.StringArray:
                    return typeof(string[]);
                case UIType.IntArray:
                    return typeof(int[]);
                case UIType.FloatArray:
                    return typeof(float[]);
                case UIType.Bool:
                    return typeof(bool);
                case UIType.BoolArray:
                    return typeof(bool[]);
                case UIType.Vector3:
                    return typeof(Vector3);
                case UIType.Vector3Array:
                    return typeof(Vector3[]);
                case UIType.Color:
                    return typeof(Color);
                case UIType.ColorArray:
                    return typeof(Color[]);
                case UIType.ChoiceExp:
                    return typeof(ChoiceExp);
                case UIType.ChoiceExpArray:
                    return typeof(ChoiceExp[]);
                case UIType.Vector2:
                    return typeof(Vector2);
                case UIType.Vector2Array:
                    return typeof(Vector2[]);
                case UIType.Ease:
                    return typeof(Ease);
                case UIType.Loop:
                    return typeof(Loop);
                case UIType.Rotate:
                    return typeof(Rotate);
                case UIType.PathMode:
                    return typeof(PathMode);
                case UIType.TextAlignmentOptions:
                    return typeof(TextAlignmentOptions);
                case UIType.TYPE:
                    return typeof(TYPE);
                case UIType.PATH:
                    return typeof(TYPE);
                case UIType.GameState:
                    return typeof(TYPE);
                case UIType.EaseArray:
                    return typeof(Ease[]);
                case UIType.LoopArray:
                    return typeof(Loop[]);
                case UIType.RotateArray:
                    return typeof(Rotate[]);
                case UIType.PathModeArray:
                    return typeof(PathMode[]);
                case UIType.TextAlignmentOptionsArray:
                    return typeof(TextAlignmentOptions[]);
                case UIType.TYPEArray:
                    return typeof(TYPE[]);
                case UIType.PATHArray:
                    return typeof(TYPE[]);
                case UIType.GameStateArray:
                    return typeof(TYPE[]);
                default:
                    return typeof(object);
            }
        }
        //通过类型获取UI种类
        public static UIType Type2UIType(Type type)
        {
            if (type == typeof(string))
            {
                return UIType.String;
            }
            else if (type == typeof(string[]))
            {
                return UIType.StringArray;
            }
            else if (type == typeof(int))
            {
                return UIType.Int;
            }
            else if (type == typeof(int[]))
            {
                return UIType.IntArray;
            }
            else if (type == typeof(float))
            {
                return UIType.Float;
            }
            else if (type == typeof(float[]))
            {
                return UIType.FloatArray;
            }
            else if (type == typeof(bool))
            {
                return UIType.Bool;
            }
            else if (type == typeof(bool[]))
            {
                return UIType.BoolArray;
            }
            else if (type == typeof(Vector2))
            {
                return UIType.Vector2;
            }
            else if (type == typeof(Vector2[]))
            {
                return UIType.Vector2Array;
            }
            else if (type == typeof(Vector3))
            {
                return UIType.Vector3;
            }
            else if (type == typeof(Vector3[]))
            {
                return UIType.Vector3Array;
            }
            else if (type == typeof(Color))
            {
                return UIType.Color;
            }
            else if (type == typeof(Color[]))
            {
                return UIType.ColorArray;
            }
            else if (type == typeof(Ease))
            {
                return UIType.Ease;
            }
            else if (type == typeof(Ease[]))
            {
                return UIType.EaseArray;
            }
            else if (type == typeof(Loop))
            {
                return UIType.Loop;
            }
            else if (type == typeof(Loop[]))
            {
                return UIType.LoopArray;
            }
            else if (type == typeof(Rotate))
            {
                return UIType.Rotate;
            }
            else if (type == typeof(Rotate[]))
            {
                return UIType.RotateArray;
            }
            else if (type == typeof(PathMode))
            {
                return UIType.PathMode;
            }
            else if (type == typeof(PathMode[]))
            {
                return UIType.PathModeArray;
            }
            else if (type == typeof(TextAlignmentOptions))
            {
                return UIType.TextAlignmentOptions;
            }
            else if (type == typeof(TextAlignmentOptions[]))
            {
                return UIType.TextAlignmentOptionsArray;
            }
            else if (type == typeof(TYPE))
            {
                return UIType.TYPE;
            }
            else if (type == typeof(TYPE[]))
            {
                return UIType.TYPEArray;
            }
            else if (type == typeof(ChoiceExp))
            {
                return UIType.ChoiceExp;
            }
            else if (type == typeof(ChoiceExp[]))
            {
                return UIType.ChoiceExpArray;
            }
            else if (type == typeof(PATH))
            {
                return UIType.PATH;
            }
            else if (type == typeof(PATH[]))
            {
                return UIType.PATHArray;
            }
            else if (type == typeof(GameState))
            {
                return UIType.GameState;
            }
            else if (type == typeof(GameState[]))
            {
                return UIType.GameStateArray;
            }
            else
            {
                return UIType.None;
            }
        }

        //获取枚举类型
        Type UIType2Enum(UIType type)
        {
            switch (type)
            {
                case UIType.Ease:
                case UIType.EaseArray:
                    {
                        return typeof(Ease);
                    }
                case UIType.Loop:
                case UIType.LoopArray:
                    {
                        return typeof(Loop);
                    }
                case UIType.Rotate:
                case UIType.RotateArray:
                    {
                        return typeof(Rotate);
                    }
                case UIType.PathMode:
                case UIType.PathModeArray:
                    {
                        return typeof(PathMode);
                    }
                case UIType.TextAlignmentOptions:
                case UIType.TextAlignmentOptionsArray:
                    {
                        return typeof(TextAlignmentOptions);
                    }
                case UIType.TYPE:
                case UIType.TYPEArray:
                    {
                        return typeof(TYPE);
                    }
                case UIType.PATH:
                case UIType.PATHArray:
                    {
                        return typeof(PATH);
                    }
                case UIType.GameState:
                case UIType.GameStateArray:
                    {
                        return typeof(GameState);
                    }
                default:
                    {
                        return null;
                    }
            }
        }

        public object GetValue(out bool success)
        {
            object result = null;
            success = true;
            switch (type)
            {
                case UIType.String:
                    {
                        result = GetString(out success);
                    }
                    break;
                case UIType.Int:
                    {
                        result = GetInt(out success);
                    }
                    break;
                case UIType.Float:
                    {
                        result = GetFloat(out success);
                    }
                    break;
                case UIType.StringArray:
                    {
                        result = GetStringArray(out success);
                    }
                    break;
                case UIType.IntArray:
                    {
                        result = GetIntArray(out success);
                    }
                    break;
                case UIType.FloatArray:
                    {
                        result = GetFloatArray(out success);
                    }
                    break;
                case UIType.Bool:
                    {
                        result = GetBool(out success);
                    }
                    break;
                case UIType.BoolArray:
                    {
                        result = GetBoolArray(out success);
                    }
                    break;
                case UIType.Vector3:
                    {
                        result = GetVector3(out success);
                    }
                    break;
                case UIType.Vector3Array:
                    {
                        result = GetVector3Array(out success);
                    }
                    break;
                case UIType.Color:
                    {
                        result = GetColor(out success);
                    }
                    break;
                case UIType.ColorArray:
                    {
                        result = GetColorArray(out success);
                    }
                    break;
                case UIType.Vector2:
                    {
                        result = GetVector2(out success);
                    }
                    break;
                case UIType.Vector2Array:
                    {
                        result = GetVector2Array(out success);
                    }
                    break;
                case UIType.ChoiceExp:
                    {
                        result = GetChoiceExp(out success);
                    }
                    break;
                case UIType.ChoiceExpArray:
                    {
                        result = GetChoiceExpArray(out success);
                    }
                    break;
                case UIType.Ease:
                case UIType.Loop:
                case UIType.Rotate:
                case UIType.PathMode:
                case UIType.TextAlignmentOptions:
                case UIType.TYPE:
                case UIType.PATH:
                case UIType.GameState:
                    {
                        result = GetEnum(out success);
                    }
                    break;
                case UIType.EaseArray:
                    {
                        result= GetEaseArray(out success);
                    }
                    break;
                default:
                    {

                    }
                    break;
            }
            return result;
        }

        //分种类获取值
        string GetString(out bool success)
        {
            success = true;
            return StringText.Text;
        }
        int GetInt(out bool success)
        {
            int result = 0;
            success = int.TryParse(StringText.Text, out result);
            return result;
        }
        float GetFloat(out bool success)
        {
            float result = 0;
            success = float.TryParse(StringText.Text, out result);
            return result;
        }
        string[] GetStringArray(out bool success)
        {
            success = StringArray.Items.Count != 0;
            string[] result = new string[StringArray.Items.Count];
            for(int i = 0; i < StringArray.Items.Count; i++)
            {
                result[i] = StringArray.Items[i].ToString();
            }
            return result;
        }
        int[] GetIntArray(out bool success)
        {
            success = true;
            int[] result = new int[StringArray.Items.Count];
            for (int i = 0; i < StringArray.Items.Count&&success; i++)
            {
                success = int.TryParse(StringArray.Items[i].ToString(), out result[i]);
            }
            return result;
        }
        float[] GetFloatArray(out bool success)
        {
            success = StringArray.Items.Count != 0;
            float[] result = new float[StringArray.Items.Count];
            for (int i = 0; i < StringArray.Items.Count && success; i++)
            {
                success = float.TryParse(StringArray.Items[i].ToString(), out result[i]);
            }
            return result;
        }
        bool GetBool(out bool success)
        {
            success = true;
            return (BoolText.IsChecked == true);
        }
        bool[] GetBoolArray(out bool success)
        {
            success = BoolArray.Items.Count!=0;
            bool[] result = new bool[BoolArray.Items.Count];
            for(int i = 0; i < BoolArray.Items.Count && success; i++)
            {
                success = bool.TryParse(BoolArray.Items[i].ToString(), out result[i]);
            }
            return result;
        }
        Vector3 GetVector3(out bool success)
        {
            Vector3 vector3 = new Vector3 { };
            success = float.TryParse(Vector3x.Text, out vector3.x);
            success &= float.TryParse(Vector3y.Text, out vector3.y);
            success &= float.TryParse(Vector3z.Text, out vector3.z);
            return vector3;
        }
        Vector3[] GetVector3Array(out bool success)
        {
            success = Vector3Array.Items.Count != 0;
            Vector3[] result = new Vector3[Vector3Array.Items.Count];
            for(int i = 0; i < Vector3Array.Items.Count && success; i++)
            {
                string[] vector = Vector3Array.Items[i].ToString().Split(new char[] { ',' });
                success = float.TryParse(vector[0], out result[i].x);
                success &= float.TryParse(vector[1], out result[i].y);
                success &= float.TryParse(vector[2], out result[i].z);
            }
            return result;
        }
        Color GetColor(out bool success)
        {
            Color color = new Color { };
            success = byte.TryParse(Colorx.Text, out color.r);
            success &= byte.TryParse(Colory.Text, out color.g);
            success &= byte.TryParse(Colorz.Text, out color.b);
            return color;
        }
        ChoiceExp GetChoiceExp(out bool success)
        {
            ChoiceExp choiceExp = new ChoiceExp { exp = ChoiceExpx.Text, jumpId = ChoiceExpy.Text, text = ChoiceExpz.Text };
            success = true;
            return choiceExp;
        }
        Color[] GetColorArray(out bool success)
        {
            success = ColorArray.Items.Count != 0;
            Color[] result = new Color[ColorArray.Items.Count];
            for (int i = 0; i < ColorArray.Items.Count && success; i++)
            {
                string[] vector = ColorArray.Items[i].ToString().Split(new char[] { ',' });
                success = byte.TryParse(vector[0], out result[i].r);
                success &= byte.TryParse(vector[1], out result[i].g);
                success &= byte.TryParse(vector[2], out result[i].b);
            }
            return result;
        }
        Vector2 GetVector2(out bool success)
        {
            Vector2 vector2 = new Vector2 { };
            success = float.TryParse(Vector2x.Text, out vector2.x);
            success &= float.TryParse(Vector2y.Text, out vector2.y);
            return vector2;
        }
        Vector2[] GetVector2Array(out bool success)
        {
            success = Vector2Array.Items.Count != 0;
            Vector2[] result = new Vector2[Vector2Array.Items.Count];
            for (int i = 0; i < Vector2Array.Items.Count && success; i++)
            {
                string[] vector = Vector2Array.Items[i].ToString().Split(new char[] { ',' });
                success = float.TryParse(vector[0], out result[i].x);
                success &= float.TryParse(vector[1], out result[i].y);
            }
            return result;
        }
        Enum GetEnum(out bool success)
        {
            Enum result = null;
            success = true;
            try
            {
                result = (Enum)Enum.Parse(UIType2Enum(type), EnumText.Text);
            }
            catch
            {
                success = false;
            }
            return result;
        }
        Ease[] GetEaseArray(out bool success)
        {
            Ease[] result = new Ease[EnumArray.Items.Count];
            success = EnumArray.Items.Count != 0;
            for(int i=0;i< EnumArray.Items.Count&&success;i++)
            {
                success = Enum.TryParse(EnumArray.Items[i].ToString(),out result[i]);
            }
            return result;
        }
        ChoiceExp[] GetChoiceExpArray(out bool success)
        {
            success = ChoiceExpArray.Items.Count != 0;
            ChoiceExp[] result = new ChoiceExp[ChoiceExpArray.Items.Count];
            for (int i = 0; i < ChoiceExpArray.Items.Count && success; i++)
            {
                string[] vector = ChoiceExpArray.Items[i].ToString().Split(new char[] { ',' });
                result[i].exp = vector[0];
                result[i].jumpId = vector[1];
                result[i].text = vector[2];
            }
            return result;
        }

        //字符串数组UI函数
        private void StringArray_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (StringArray.SelectedIndex != -1)
            {
                StringArrayText.Text = StringArray.SelectedItem.ToString();
            }
            else
            {
                StringArrayText.Text = "";
            }
        }
        private void StringArrayAdd_Click(object sender, RoutedEventArgs e)
        {
            StringArray.Items.Insert(StringArray.SelectedIndex + 1, StringArrayText.Text.ToString());
        }
        private void StringArrayMod_Click(object sender, RoutedEventArgs e)
        {
            if (StringArray.SelectedIndex != -1)
            {
                int index = StringArray.SelectedIndex;
                StringArray.Items[index] = StringArrayText.Text.ToString();
                StringArray.SelectedIndex = index;
            }
        }
        private void StringArrayDel_Click(object sender, RoutedEventArgs e)
        {
            if (StringArray.SelectedIndex != -1)
            {
                StringArray.Items.RemoveAt(StringArray.SelectedIndex);
            }
        }

        //布尔型数组
        private void BoolArray_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (BoolArray.SelectedIndex != -1)
            {
                BoolArrayText.IsChecked = bool.Parse(BoolArray.SelectedItem.ToString());
            }
            else
            {
                BoolArrayText.IsChecked = false;
            }
        }
        private void BoolArrayAdd_Click(object sender, RoutedEventArgs e)
        {
            BoolArray.Items.Insert(BoolArray.SelectedIndex + 1, BoolArrayText.IsChecked.ToString());
        }
        private void BoolArrayMod_Click(object sender, RoutedEventArgs e)
        {
            if (BoolArray.SelectedIndex != -1)
            {
                int index = BoolArray.SelectedIndex;
                BoolArray.Items[index] = BoolArrayText.IsChecked.ToString();
                BoolArray.SelectedIndex = index;
            }
        }
        private void BoolArrayDel_Click(object sender, RoutedEventArgs e)
        {
            if (BoolArray.SelectedIndex != -1)
            {
                BoolArray.Items.RemoveAt(BoolArray.SelectedIndex);
            }
        }

        //三元组数组
        private void Vector3Array_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Vector3Array.SelectedIndex != -1)
            {
                string obj = (string)Vector3Array.SelectedItem;
                string[] objs = obj.Split(new char[] { ',' });
                Vector3Arrayx.Text = objs[0];
                Vector3Arrayy.Text = objs[1];
                Vector3Arrayz.Text = objs[2];
            }
            else
            {
                Vector3Arrayx.Text = "";
                Vector3Arrayy.Text = "";
                Vector3Arrayz.Text = "";
            }
        }
        private void Vector3ArrayAdd_Click(object sender, RoutedEventArgs e)
        {
            Vector3Array.Items.Insert(Vector3Array.SelectedIndex + 1, Vector3Arrayx.Text + "," + Vector3Arrayy.Text + "," + Vector3Arrayz.Text);
        }
        private void Vector3ArrayMod_Click(object sender, RoutedEventArgs e)
        {
            if (Vector3Array.SelectedIndex != -1)
            {
                int index = Vector3Array.SelectedIndex;
                Vector3Array.Items[index] = Vector3Arrayx.Text + "," + Vector3Arrayy.Text + "," + Vector3Arrayz.Text;
                Vector3Array.SelectedIndex = index;
            }
        }
        private void Vector3ArrayDel_Click(object sender, RoutedEventArgs e)
        {
            if (Vector3Array.SelectedIndex != -1)
            {
                Vector3Array.Items.RemoveAt(Vector3Array.SelectedIndex);
            }
        }

        //二元组数组
        private void Vector2Array_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Vector2Array.SelectedIndex != -1)
            {
                string obj = (string)Vector2Array.SelectedItem;
                string[] objs = obj.Split(new char[] { ',' });
                Vector2Arrayx.Text = objs[0];
                Vector2Arrayy.Text = objs[1];
            }
            else
            {
                Vector2Arrayx.Text = "";
                Vector2Arrayy.Text = "";
            }
        }
        private void Vector2ArrayAdd_Click(object sender, RoutedEventArgs e)
        {
            Vector2Array.Items.Insert(Vector2Array.SelectedIndex + 1, Vector2Arrayx.Text + "," + Vector2Arrayy.Text);
        }
        private void Vector2ArrayMod_Click(object sender, RoutedEventArgs e)
        {
            if (Vector2Array.SelectedIndex != -1)
            {
                int index = Vector2Array.SelectedIndex;
                Vector2Array.Items[index] = Vector2Arrayx.Text + "," + Vector2Arrayy.Text;
                Vector2Array.SelectedIndex = index;
            }
        }
        private void Vector2ArrayDel_Click(object sender, RoutedEventArgs e)
        {
            if (Vector2Array.SelectedIndex != -1)
            {
                Vector2Array.Items.RemoveAt(Vector2Array.SelectedIndex);
            }
        }

        //枚举型数组
        private void EnumArray_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (EnumArray.SelectedIndex != -1)
            {
                string obj = (string)EnumArray.SelectedItem;
                for(int i = 0; i < EnumArrayText.Items.Count; i++)
                {
                    if (obj == (string)EnumArrayText.Items[i])
                    {
                        EnumArrayText.SelectedIndex = i;
                    }
                }
            }
            else
            {
                EnumArrayText.SelectedIndex = -1;
            }
        }
        private void EnumArrayAdd_Click(object sender, RoutedEventArgs e)
        {
            EnumArray.Items.Insert(EnumArray.SelectedIndex + 1, (string)EnumArrayText.SelectedItem);
        }
        private void EnumArrayMod_Click(object sender, RoutedEventArgs e)
        {
            if (EnumArray.SelectedIndex != -1)
            {
                int index = EnumArray.SelectedIndex;
                EnumArray.Items[index] = (string)EnumArrayText.SelectedItem;
                EnumArray.SelectedIndex = index;
            }
        }
        private void EnumArrayDel_Click(object sender, RoutedEventArgs e)
        {
            if (EnumArray.SelectedIndex != -1)
            {
                EnumArray.Items.RemoveAt(EnumArray.SelectedIndex);
            }
        }

        //颜色数组
        private void ColorArray_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ColorArray.SelectedIndex != -1)
            {
                string obj = (string)ColorArray.SelectedItem;
                string[] objs = obj.Split(new char[] { ',' });
                ColorArrayx.Text = objs[0];
                ColorArrayy.Text = objs[1];
                ColorArrayz.Text = objs[2];
            }
            else
            {
                ColorArrayx.Text = "";
                ColorArrayy.Text = "";
                ColorArrayz.Text = "";
            }
        }
        private void ColorArrayAdd_Click(object sender, RoutedEventArgs e)
        {
            ColorArray.Items.Insert(ColorArray.SelectedIndex + 1, ColorArrayx.Text + "," + ColorArrayy.Text + "," + ColorArrayz.Text);
        }
        private void ColorArrayMod_Click(object sender, RoutedEventArgs e)
        {
            if (ColorArray.SelectedIndex != -1)
            {
                int index = ColorArray.SelectedIndex;
                ColorArray.Items[index] = ColorArrayx.Text + "," + ColorArrayy.Text + "," + ColorArrayz.Text;
                ColorArray.SelectedIndex = index;
            }
        }
        private void ColorArrayDel_Click(object sender, RoutedEventArgs e)
        {
            if (ColorArray.SelectedIndex != -1)
            {
                ColorArray.Items.RemoveAt(ColorArray.SelectedIndex);
            }
        }

        //选项数组
        private void ChoiceExpArray_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ChoiceExpArray.SelectedIndex != -1)
            {
                string obj = (string)ChoiceExpArray.SelectedItem;
                string[] objs = obj.Split(new char[] { ',' });
                ChoiceExpArrayx.Text = objs[0];
                ChoiceExpArrayy.Text = objs[1];
                ChoiceExpArrayz.Text = objs[2];
            }
            else
            {
                ChoiceExpArrayx.Text = "";
                ChoiceExpArrayy.Text = "";
                ChoiceExpArrayz.Text = "";
            }
        }
        private void ChoiceExpArrayAdd_Click(object sender, RoutedEventArgs e)
        {
            ChoiceExpArray.Items.Insert(ChoiceExpArray.SelectedIndex + 1, ChoiceExpArrayx.Text + "," + ChoiceExpArrayy.Text + "," + ChoiceExpArrayz.Text);
        }
        private void ChoiceExpArrayMod_Click(object sender, RoutedEventArgs e)
        {
            if (ChoiceExpArray.SelectedIndex != -1)
            {
                int index = ChoiceExpArray.SelectedIndex;
                ChoiceExpArray.Items[index] = ChoiceExpArrayx.Text + "," + ChoiceExpArrayy.Text + "," + ChoiceExpArrayz.Text;
                ChoiceExpArray.SelectedIndex = index;
            }
        }
        private void ChoiceExpArrayDel_Click(object sender, RoutedEventArgs e)
        {
            if (ChoiceExpArray.SelectedIndex != -1)
            {
                ChoiceExpArray.Items.RemoveAt(ChoiceExpArray.SelectedIndex);
            }
        }
    }
}
